package com.aaa.jx.controller;

import com.aaa.jx.entity.TSdsPlan;
import com.aaa.jx.entity.TSdsSub;
import com.aaa.jx.service.TSdsPlanService;
import com.aaa.jx.service.impl.TSdsPlanServiceImpl;

import com.util.LayData;
import com.util.LayPage;
import com.util.PrintJsonData;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.List;
import java.util.Map;

/**
 * (TSdsPlan)表控制层
 *
 * @author makejava
 * @since 2023-05-04 11:19:00
 */


@WebServlet(name = "TSdsPlanServlet", value = "/TSdsPlanServlet")
public class TSdsPlanServlet extends HttpServlet {

    private TSdsPlanService tSdsPlanService = new TSdsPlanServiceImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Map<String, String[]> map = req.getParameterMap();
        TSdsPlan tSdsPlan=new TSdsPlan();
        try {
            BeanUtils.populate(tSdsPlan,map);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        }
        // 
        String method = req.getParameter("method");
        if (method==null){
            String planname = req.getParameter("planname");
            List<TSdsPlan> tSdsPlans = tSdsPlanService.queryByPlan(planname);
            LayData success=new LayData(0,"",0L,tSdsPlans);
            PrintJsonData.printData(resp,success);
        }else if(method.equals("query")){
            String page = req.getParameter("page");
            String limit = req.getParameter("limit");
            LayPage layPage=new LayPage(page,limit);
            layPage.setStart(layPage.getStart());
            List<TSdsPlan> tSdsPlans = tSdsPlanService.queryByPage(null, layPage);
            long count = tSdsPlanService.count(null);
            LayData success=new LayData(0,"",count,tSdsPlans);
            PrintJsonData.printData(resp,success);
            //query(req,resp);
            return;
        }else if(method.equals("add")){
            tSdsPlanService.insert(tSdsPlan);
            LayData success=new LayData( 0L,null);
            PrintJsonData.printData(resp,success);
//            add(req,resp);
            return;
        }else if(method.equals("upd")){
            tSdsPlanService.update(tSdsPlan);
            LayData success=new LayData(0,"",0L,null);
            PrintJsonData.printData(resp,success);
            //upd(req,resp);
            return;
        }else if(method.equals("del")){
            del(req,resp);
            return;
        }else if(method.equals("getByCode")){
            String code = req.getParameter("code");
            TSdsPlan tSdsPlan1 = tSdsPlanService.queryByCode(code);
            LayData success=new LayData(0,"",0L,tSdsPlan1);
            PrintJsonData.printData(resp,success);
            return;
        }else if(method.equals("check")){
            String code = req.getParameter("code");
            LayData success=new LayData();
            TSdsPlan tSdsPlan1 = tSdsPlanService.queryByCode(code);
            System.out.println(tSdsPlan1);
            if(tSdsPlan1!=null){
                success.setCode(1);
            }else {
                success.setCode(0);
            }
            PrintJsonData.printData(resp,success);
        }

    }

  

    private void del(HttpServletRequest req, HttpServletResponse resp) {
    }

    private void upd(HttpServletRequest req, HttpServletResponse resp) {
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
    }

    private void query(HttpServletRequest req, HttpServletResponse resp) {

    }
}




